<?php
class Admin extends CI_Controller {
  
  function __construct() {
    parent::__construct ();
    $this->load->model ( 'Admin_Model' );
  }
  function index() {
    $this->load->view ( 'login' );
  }
  function login() {
    $post = $this->input->post ();
    $username = $post ['username'];
    $password = $post ['password'];
    $id_login = $this->Admin_Model->login ( $username, $password );
    if (isset($id_login)) {
      $this->session->set_userdata('user_login',array('login_flag' =>true,'login_id'=>$id_login));
      $this->session->set_flashdata('success', 'ログイン成功');
      redirect($this->config->base_url('index.php/user'));
    } else {
      $this->session->set_flashdata('unsuccess', 'ログイン失敗');
      redirect($this->config->base_url('index.php/admin'));
    }
  }
  function logout() {
    // delete session
    $this->session->sess_destroy();
    $this->load->view ( 'login' );
  }
  function change_password_admin($admin_id) {
    $data = null;
    if ($this->input->server ( 'REQUEST_METHOD' ) == 'GET') {
      $this->load->view ( 'common/header' );
      $this->load->view ( 'change_password_admin');
      $this->load->view ( 'common/footer' );
    } else {
      $this->load->library('form_validation');
      $data = array();
      if ($this->form_validation->run('admin/change_password_admin') === FALSE)
      {
        $this->form_validation->set_error_delimiters('<div class="error" style="color:red">', '</div>');
        $this->load->view ( 'common/header' );
        $this->load->view ( 'change_password_admin');
        $this->load->view ( 'common/footer' );
      } else {
          $update_admin_data = $this->input->post ();
          // var_dump($update_admin_data);die;
          $update_results = $this->Admin_Model->change_password_admin ( $admin_id, $update_admin_data );
          if ($update_results) {
            $this->session->set_flashdata('success', '管理者のパスワード更新失敗');
            redirect ( $this->config->base_url ( 'index.php/admin/logout' ) );
          } else {
            $this->session->set_flashdata('unsuccess', '管理者のパスワード更新失敗');
            redirect ( $this->config->base_url ( 'index.php/admin/change_password_admin' ) . "/" . $admin_id );
          }
      }
    }
  }
  function help() {
      $this->load->view ( 'common/header' );
      $this->load->view ( 'help');
      $this->load->view ( 'common/footer' );
  }
}
?>